<?php
/*
	本插件：$this->model('api')->方法名();
	其他地方调用：model('jy_kuaidisys/api')->方法名();
	https://www.showdoc.com.cn/yunyangwl  密码：yunyangwl 
*/
namespace jy_kuaidisys;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	public function request($serviceCode='',$content=array('form'=>'JY')){
		$appid=$this->setting['appid'];
		$secretKey=$this->setting['key'];
		if(!$appid || !$secretKey){
			exi('请前往：基本设置--填写：快递接口APPID,快递接口密钥！','error');
		}
		if($this->setting['istest']){
			$url='https://api.yunyangwl.com/api/sandbox/openService';
		}else{
			$url='https://api.yunyangwl.com/api/wuliu/openService';
		}
		$timeStamp=time();
		$post=array(
			'serviceCode'=>$serviceCode,
			'timeStamp'=>$timeStamp*1000,
			'requestId'=>$_SESSION['uid'],
			'appid'=>$appid,
		);
		$sign=$this->sign($post,$secretKey);
		$post['sign']=$sign;
		$post['content']=$content;
		$post=ijson_encode($post);
		//file_put_contents(ROOT_D.'/1.txt',$post);
		$res = cfc('http')->ihttp_json($url,$post);
		//print_r($res);exit;
		$content=json_decode($res['content'],true);
		return $content;
	}
	public function upkdlist(){
		$res=$this->request('QUERY_ALL_CHANNEL');
		//print_r($res);exit;
		if($res['code']!=200){
			exi('获取失败；'.$res['code'].'，'.$res['message'],'error');
		}
		return $res['result'];
	}
	public function getfeekd($content=array()){
		$res=$this->request('CHECK_CHANNEL_INTELLECT',$content);
		//print_r($res);exit;
		if($res['code']!=1){
			exi('获取快递公司失败；'.$res['code'].'，'.$res['message'],'error');
		}
		return $res['result'];
	}
	public function getprice($content=array()){
		$res=$this->request('QUERY_ORIGINAL_PRICE',$content);
		if($res['code']!=1){
			exi('获取价格失败；'.$res['code'].'，'.$res['message'],'error');
		}
		return $res['result'];
	}
	public function addorder($content=array(),$isexi=false){
		$res=$this->request('ADD_BILL_INTELLECT',$content);
		if($res['code']!=1 && $isexi){
			exi('下单失败；'.$res['code'].'，'.$res['message'],'error');
		}
		return $res;
	}
	//取消订单
	public function cancel($content=array(),$isexi=false){
		$res=$this->request('CANCEL',$content);
		if($res['code']!=1 && $res['code'] && $isexi){
			exi('取消订单失败；'.$res['code'].'，'.$res['message'],'error');
		}
		return $res;
	}
	//查询运单轨迹接口
	public function getguiji($content=array()){
		$res=$this->request('QUERY_TRANCE',$content);
		if($res['code']!=1){
			exi('获取运单轨迹失败；'.$res['code'].'，'.$res['message'],'error');
		}
		return $res['result'];
	}
	public function sign($post=array(),$secretKey=''){
		$params=array(
			'timeStamp'=>$post['timeStamp'],
			'requestId'=>$post['requestId'],
			'appid'=>$post['appid'],
		);
		ksort($params);
		$sign_str='';
		foreach($params as $key=>$value){
			$sign_str.=$value;
		}
		$sign_str.=$secretKey;
		return  md5($sign_str);
	}
}
?>